//
//  ZDBWithdrawalActionViewController.m
//  ZDB
//
//  Created by 微笑吧阳光 on 2020/5/28.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBWithdrawalActionViewController.h"
#import "ZDBTiXianHeaderView.h"
#import "TixianItemView.h"
#import "ZDBBindingAlipayViewController.h"
#import "ZDBTransferListViewController.h"
@interface ZDBWithdrawalActionViewController ()<UINavigationControllerDelegate>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) ZDBTiXianHeaderView * tableViewHeader;
@property (nonatomic, strong) UIView *btnViewBackground;
@property (nonatomic, strong) UIView * zhuanzhangViewBackground;
@property (nonatomic, strong) UIButton * actionBtn;
@property (nonatomic, strong) UILabel  *  tishiLab;
@property (nonatomic, strong) UIScrollView  *  contentBgSc;
@property (assign, nonatomic) NSInteger   itemSeclectIndex;
@property (assign, nonatomic) NSInteger   chooseSeclectIndex;
@property (nonatomic,strong) UITextField * amountField;//转账金额
@property (nonatomic,strong) NSString * tpid;//平台id
@end

@implementation ZDBWithdrawalActionViewController
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
 // 判断要显示的控制器是否是自己
  BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
 
 [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableViewHeader.frame = CGRectMake(0, 0, kScreenWidth, 410 );
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.itemSeclectIndex = 0;
    self.chooseSeclectIndex = 0;
    self.dataSource = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.contentBgSc = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:self.contentBgSc];
    
    
    self.tableViewHeader = [[NSBundle mainBundle] loadNibNamed:@"ZDBTiXianHeaderView" owner:self options:nil].lastObject;
    self.tableViewHeader.frame = CGRectMake(0, 0, kScreenWidth, 410);
    [self.contentBgSc addSubview:self.tableViewHeader];
    [self config_HeaderChange];
    NSDictionary * userDic = [[DJLoginHelper sharedInstance] cj_UserDicc];
    self.tableViewHeader.zhifubaoName.text = userDic[@"zfbName"];
    self.tableViewHeader.zhifubao.text = userDic[@"zfbNumber"];
    
    
    [self.tableViewHeader.headerImage  sd_setImageWithURL:[NSURL URLWithString:[userDic jsonString:@"headImage"]] placeholderImage:PlaceHolderImage];
    UIView *btnViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableViewHeader.bottom, kScreenWidth, 80)];
        btnViewBackground.backgroundColor = [UIColor whiteColor];
    [self.contentBgSc addSubview:btnViewBackground];
    self.btnViewBackground = btnViewBackground;
    self.btnViewBackground.hidden = YES;
    
    self.zhuanzhangViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableViewHeader.bottom, kScreenWidth, 80)];
    self.zhuanzhangViewBackground.backgroundColor = [UIColor whiteColor];
    [self.contentBgSc addSubview:self.zhuanzhangViewBackground];
    self.zhuanzhangViewBackground.hidden = NO;
    
    
    WEAKSELF
    [self.tableViewHeader.editBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        ZDBBindingAlipayViewController * vcc = [ZDBBindingAlipayViewController new];
        vcc.title = @"编辑支付宝";
        vcc.updetaInfoClickBlock = ^(BOOL isUpdate) {
            [strongSelf updateZhifubao];
        };
        [strongSelf.navigationController pushViewController:vcc animated:YES];
        
     }];
    
    
    [self.tableViewHeader.backBTn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        [strongSelf.navigationController popViewControllerAnimated:YES];
     }];
    [self.tableViewHeader.transferBtn add_BtnClickHandler:^(NSInteger tag) { //转账记录
        STRONGSELFFor(weakSelf);
        ZDBTransferListViewController * transferListVC = [[ZDBTransferListViewController alloc] init];
        transferListVC.title =@"转账记录";
        [strongSelf.navigationController pushViewController:transferListVC animated:YES];
      
    }];
    
    self.actionBtn = [BBControl createButtonWithFrame:CGRectMake(15,  self.btnViewBackground.bottom+15, kScreenWidth-30, 50) target:self SEL:@selector(actionBtnClick:) title:@""];
    [self.actionBtn setImage:[UIImage imageNamed:@"action_btn"] forState:0];
    [self.contentBgSc addSubview:self.actionBtn];
    
    
    self.tishiLab= [BBControl createLabelWithFrame:CGRectMake(15, self.actionBtn.bottom+15, kScreenWidth-30, 100) Font:12 Text:@"说明：\n1.微信必须实名认证，否则提现将无法到账。\n2.周一至周五24小时审批到账，周末不处理，请耐心等待。\n3.提现手续费3%"];
    self.tishiLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [self.contentBgSc addSubview:self.tishiLab];
    self.tishiLab.hidden = YES;
    [self config_zhuanzhangViewBackground];
    self.contentBgSc.contentSize = CGSizeMake(kScreenWidth, self.tishiLab.bottom + 50);
    [self transferAccount];//获取转账账户
    [self getDataRefush];
    [self updateZhifubao];
    
}
-(void)config_zhuanzhangViewBackground{
    
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 10, 200, 20)];
    titleLab.text = @"转账金额";
    titleLab.font = AutoFont(16);
    titleLab.textColor = ColorStr(@"#333333");
    [self.zhuanzhangViewBackground addSubview:titleLab];
    _amountField = [[UITextField alloc] initWithFrame:CGRectMake(26, titleLab.bottom +10,self.zhuanzhangViewBackground.width - 52, 30)];
    _amountField.placeholder = @"请输入转账金额";
    _amountField.font = AutoBoldFont(14);
    _amountField.textColor = ColorStr(@"#333333");
    _amountField.tintColor = TABTITLECOLOR;
    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(_amountField, ivar);
    placeholderLabel.textColor = ColorStr(@"#989898");
    placeholderLabel.font = AutoFont(12);
    _amountField.keyboardType = UIKeyboardTypeDecimalPad;
    [self.zhuanzhangViewBackground addSubview:_amountField];
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(15 , self.zhuanzhangViewBackground.height-0.5)];
    [bezierPath addLineToPoint:CGPointMake(UIScreenWidth-15 , self.zhuanzhangViewBackground.height-0.5)];
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = RGBA(230, 230, 230, 1).CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.lineWidth = 0.5f;
    [self.zhuanzhangViewBackground.layer addSublayer:shapeLayer];
    
}
-(void)config_HeaderChange{
    
    self.tableViewHeader.line1.backgroundColor = [UIColor colorWithHexString:@"#ffe98f"];
    [self.tableViewHeader.zhuanzhangBtn setTitleColor:[UIColor colorWithHexString:@"#ffe98f"] forState:0];
    self.tableViewHeader.line2.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [self.tableViewHeader.tixianBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:0];
    
    self.tableViewHeader.line1.hidden =
    self.tableViewHeader.transferBtn.hidden =
    self.tableViewHeader.tixianActionBg.hidden = NO;
    
    self.tableViewHeader.line2.hidden = self.tishiLab.hidden = YES;
 
   
    WEAKSELF
    [self.tableViewHeader.zhuanzhangBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        strongSelf.tableViewHeader.line1.hidden =
        self.tableViewHeader.transferBtn.hidden =
        strongSelf.tableViewHeader.tixianActionBg.hidden =
        strongSelf.zhuanzhangViewBackground.hidden = NO;
        
        strongSelf.tableViewHeader.line2.hidden =
        strongSelf.btnViewBackground.hidden =
        strongSelf.tishiLab.hidden = YES;
        
        strongSelf.tableViewHeader.line1.backgroundColor = [UIColor colorWithHexString:@"#ffe98f"];
        [strongSelf.tableViewHeader.zhuanzhangBtn setTitleColor:[UIColor colorWithHexString:@"#ffe98f"] forState:0];
        strongSelf.tableViewHeader.line2.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [strongSelf.tableViewHeader.tixianBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:0];
        
        strongSelf.actionBtn.top = strongSelf.zhuanzhangViewBackground.bottom + 15;
        strongSelf.tishiLab.top = strongSelf.actionBtn.bottom + 15;
        strongSelf.actionBtn.enabled = YES;
        strongSelf.chooseSeclectIndex = 0;
        
    }];
    
    
    [self.tableViewHeader.tixianBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        [strongSelf getIsDrawingInfo];
       
        strongSelf.tableViewHeader.line2.hidden = NO;
        strongSelf.tableViewHeader.line1.hidden = self.tableViewHeader.transferBtn.hidden = YES;
        strongSelf.tishiLab.hidden = NO;
        strongSelf.tableViewHeader.line2.backgroundColor = [UIColor colorWithHexString:@"#ffe98f"];
        [strongSelf.tableViewHeader.tixianBtn setTitleColor:[UIColor colorWithHexString:@"#ffe98f"] forState:0];
        strongSelf.tableViewHeader.line1.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [strongSelf.tableViewHeader.zhuanzhangBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:0];
        strongSelf.tableViewHeader.tixianActionBg.hidden = YES;
        strongSelf.btnViewBackground.hidden = NO;
        strongSelf.zhuanzhangViewBackground.hidden = YES;
        strongSelf.actionBtn.top = strongSelf.btnViewBackground.bottom + 15;
        strongSelf.tishiLab.top = strongSelf.actionBtn.bottom + 15;
         strongSelf.chooseSeclectIndex = 1;
    }];
    
}
- (void)transferAccount
{
    WEAKSELF
    [self showLoadingAnimation];
    NSString  * banber = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"transferAccount"];
    [[SmileHttpTool sharedInstance] POST:banber parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        STRONGSELFFor(weakSelf);
        if (statusCode ==KNetworkSuccess) {
            NSArray * list = responseObject;
            NSDictionary * dic;
            if (list.count>0) {
                dic= list[0];
                strongSelf.tableViewHeader.accountValueLab.text = [dic jsonString:@"platformName"];
                strongSelf.tableViewHeader.accountcontentLab.text = [dic jsonString:@"giveRatioInfo"];
                strongSelf.tableViewHeader.passwField.text = [dic jsonString:@"passWord"];
                strongSelf.tableViewHeader.accountValue.text = [dic jsonString:@"userName"];
                strongSelf.tableViewHeader.urlValue.text = [dic jsonString:@"dowloadUrl"];
                strongSelf.tpid = [dic jsonString:@"tpid"]; 
            }
            
            
        }else{
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
         [self stopLoadingAnimation];
    } failure:^(id responseObject, NSError *error) {
         [self stopLoadingAnimation];
        [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
        
    }];
}
-(void)updateZhifubao{
    NSDictionary * userDic = [[DJLoginHelper sharedInstance] cj_UserDicc];

    if (kStringIsEmpty(userDic[@"zfbName"]) || kStringIsEmpty(userDic[@"zfbNumber"])) {
        [JMNotifyView showNotify:@"请绑定支付宝再提现" isSuccessful:NO];
        
        self.actionBtn.enabled = NO;
    }else{
        self.actionBtn.enabled = YES;
        self.tableViewHeader.zhifubaoName.text = userDic[@"zfbName"];
        self.tableViewHeader.zhifubao.text = userDic[@"zfbNumber"];
    }
    
   
    self.tableViewHeader.cashNumber.text = [NSString stringWithFormat:@"¥%@",[userDic jsonString:@"money"]];
}
-(void)getDataRefush{

    WEAKSELF
          NSString  * banber = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getDrawingMoneyList"];
          [[SmileHttpTool sharedInstance] POST:banber parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
              STRONGSELFFor(weakSelf);
              if (statusCode ==KNetworkSuccess) {
                  
                  strongSelf.dataSource = [NSMutableArray  arrayWithArray:responseObject];
                  [self changeFreamsView];
              }else{
                  
              }
    //
        } failure:^(id responseObject, NSError *error) {
             
              [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
        }];
    
}

-(void)changeFreamsView{
    
        CGFloat Button_Width = (kScreenWidth - 60)/3;
        CGFloat Button_Height = 50;
        for (int i = 0; i < self.dataSource.count; i ++) {
           NSInteger index = i % 3;
            NSInteger page = i / 3;

            TixianItemView *mapBtn =   [[TixianItemView alloc]init];
             mapBtn.frame = CGRectMake(index * (Button_Width + 15) + 15, page  * (Button_Height + 15)+10, Button_Width, Button_Height);
            mapBtn.backgroundColor = [UIColor clearColor];
            NSDictionary * dicc = self.dataSource[i];
            mapBtn.titleLab.text = [NSString stringWithFormat:@"%@元",[dicc jsonString:@"money"]];
            mapBtn.subTitleLab.text = [NSString stringWithFormat:@"实际到账%@元",[dicc jsonString:@"arriveMoney"]];
            mapBtn.selectBtn.tag = 100+i;
            [mapBtn.selectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            mapBtn.tag = 1000+i;
            [self.btnViewBackground addSubview:mapBtn];
            if (i == 0) {
                mapBtn.isSeclec = YES;
                self.itemSeclectIndex =mapBtn.selectBtn.tag;
            }
            if (i == self.dataSource.count-1) {
                self.btnViewBackground.height =mapBtn.bottom +15;
            }
            
        }

    self.actionBtn.top = self.btnViewBackground.bottom + 15;
    self.tishiLab.top = self.actionBtn.bottom + 15;
    self.contentBgSc.contentSize = CGSizeMake(kScreenWidth, self.tishiLab.bottom + 50);
    
}
-(void)getIsDrawingInfo{
    [self showLoadingAnimation];
    WEAKSELF
          NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getIsDrawingInfo"];
          [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
              NSLog(@"--getIsDrawingInfo--%@",responseObject);
              [self stopLoadingAnimation];
              STRONGSELFFor(weakSelf);
              if (statusCode==0) {
                  
                 strongSelf.actionBtn.enabled = YES;
//                  NSString * number = [NSString stringWithFormat:@"%@",responseObject];
//                  strongSelf.tableViewHeader.cashNumber.text = [NSString stringWithFormat:@"%@元",number];
              }else{
                  [JMNotifyView showNotify:message isSuccessful:NO];
                  if (strongSelf.chooseSeclectIndex==1)
                  {
                       strongSelf.actionBtn.enabled = NO;
                  }
                 
                  
              }
        
              
          } failure:^(id responseObject, NSError *error) {
            STRONGSELFFor(weakSelf);
               [self stopLoadingAnimation];
             strongSelf.actionBtn.enabled = NO;
          }];
    
    
}

-(void)actionBtnClick:(UIButton *)sender{
    if (self.chooseSeclectIndex==0) {//转账
        
        [self transferMoney];
    }else //体现
    {
        [self addDrawingInfoDrawingInfo];
    }
}
#pragma ========转账
- (void)transferMoney
{
    if (kStringIsEmpty(_amountField.text)) {
        [JMNotifyView showNotify:@"请输入转账金额" isSuccessful:NO];
        return;
    }
    if (kStringIsEmpty(_tpid)) {
        [JMNotifyView showNotify:@"暂无收款账户" isSuccessful:NO];
        return;
    }
    
     [self showLoadingAnimation];
    WEAKSELF
       NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"transferMoney"];
       NSMutableDictionary * dataDicc = [NSMutableDictionary dictionary];
       [dataDicc setValue:_amountField.text forKey:@"money"];
       [dataDicc setValue:_tpid forKey:@"tpid"];
       [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:dataDicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
           STRONGSELFFor(weakSelf);
           [self stopLoadingAnimation];
           if (statusCode==0) {
               
               [JMNotifyView showNotify:@"转账成功" isSuccessful:YES];
               [strongSelf.navigationController popViewControllerAnimated:YES];
           }else{
               [JMNotifyView showNotify:message isSuccessful:NO];
           }
       } failure:^(id responseObject, NSError *error) {
           STRONGSELFFor(weakSelf);
           [self stopLoadingAnimation];
         
       }];
}
#pragma ========提现
- (void)addDrawingInfoDrawingInfo
{
    if (self.itemSeclectIndex ==0) {
        
        return;
    }
    NSDictionary * dicc = self.dataSource[self.itemSeclectIndex-100];
    [self showLoadingAnimation];
    WEAKSELF
    NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"addDrawingInfoDrawingInfo"];
    NSMutableDictionary * dataDicc = [NSMutableDictionary dictionary];
    [dataDicc setValue:[dicc jsonString:@"money"] forKey:@"money"];
    [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:dataDicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        STRONGSELFFor(weakSelf);
        NSLog(@"--getIsDrawingInfo--%@",responseObject);
        [self stopLoadingAnimation];
        strongSelf.actionBtn.enabled = YES;
        
        if (statusCode==0) {
            
            [JMNotifyView showNotify:@"提款成功，请关注提款信息" isSuccessful:YES];
            [strongSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
    } failure:^(id responseObject, NSError *error) {
        STRONGSELFFor(weakSelf);
        [self stopLoadingAnimation];
        strongSelf.actionBtn.enabled = YES;
    }];
}
-(void)btnClick:(UIButton *)sender{
    
    TixianItemView *mapBtn = [self.btnViewBackground viewWithTag:sender.tag-100+1000];
    self.itemSeclectIndex = sender.tag;
    for (UIView * subV  in self.btnViewBackground.subviews) {
        
        if ([subV isKindOfClass:[TixianItemView class]]) {
            TixianItemView * btnsView  = (TixianItemView *)subV;
            if (btnsView.tag == mapBtn.tag) {
                btnsView.isSeclec = YES;
            }else{
                btnsView.isSeclec = NO;
            }
        }
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
